<?php

        require_once "../../konfigurasi.php";
        
        koneksi_buka();
        
        if(isset($_GET['id'])){
                $id = $_GET['id'];
        }
        
        if(isset($_POST['id'])){
                $id = $_POST['id'];
        }
        
        if(isset($_GET['soal_id'])){
                $soal_id = $_GET['soal_id'];
        }
        
        if(isset($_POST['soal_id'])){
                $soal_id = $_POST['soal_id'];
        }
        
        if(isset($_POST['no'])){
                $no = $_POST['no'];
        }
        
        
        if(isset($_GET['no'])){
                $no = $_GET['no'];
        }
        
        if(isset($_POST['jawaban'])){
                $jawaban = $_POST['jawaban'];
        }
        
        if(isset($_POST['pertanyaan'])){
                $pertanyaan = $_POST["pertanyaan"];
        }
        
        if(isset($_POST['pilihan_a'])){
                $pilihan_a = $_POST['pilihan_a'];
        }
        if(isset($_POST['pilihan_b'])){
                $pilihan_b = $_POST['pilihan_b'];
        }
        if(isset($_POST['pilihan_c'])){
                $pilihan_c = $_POST['pilihan_c'];
        }
        if(isset($_POST['pilihan_d'])){
                $pilihan_d = $_POST['pilihan_d'];
        }       
        if(isset($_GET['operation'])){
                $operation = $_GET['operation'];
        }
        
        if(isset($_POST['operation'])){
                $operation = $_POST['operation'];
        }               
        $tgl_daftar = date("Y-m-d");
        $wkt = date("H:i:s");
        
        if($operation=="get_data"){
				$q_d_soal = mysql_query("select *, b.id as id from pilihan a left join d_soal b on a.d_soal = b.id where b.no='$no' and soal_id='$soal_id'");
				$i=0;
				$ada = false;
				while($dt = mysql_fetch_array($q_d_soal)) {
					$d_pilihan[$i] = $dt['nama'];
					$i++;
					$ada = true;
					$id = $dt['id'];
					$d_pertanyaan = $dt['pertanyaan'];
					$d_jawaban = $dt['jawaban_pilihan_no'];
				}
				
				if($ada){
					$d_soals[] = array('pertanyaan' => $d_pertanyaan, 'pilihan_a' =>$d_pilihan[0], 'pilihan_b' => $d_pilihan[1], 'pilihan_c' => $d_pilihan[2], 'pilihan_d' => $d_pilihan[3], 'jawaban' => $d_jawaban, 'id' => $id);
						$hasil = array('soals'=>$d_soals);
						echo json_encode($hasil);

				}else{
					echo 'failed';
				}
				
        }else if($operation == "add") {
				
				
                $q = "INSERT INTO d_soal
                        (no, jawaban_pilihan_no, pertanyaan, soal_id) 
                        VALUES('$no', '$jawaban', '$pertanyaan', '$soal_id')";
                
                $result = mysql_query($q);
                
                if($result){
                
					$d_soal = mysql_insert_id();
					$q_pilihan_a = "INSERT INTO pilihan (no, nama, d_soal) values(1, '$pilihan_a', $d_soal)";
					$result = mysql_query($q_pilihan_a);
					if($result){
						$q_pilihan_b = "INSERT INTO pilihan (no, nama, d_soal) values(2, '$pilihan_b', $d_soal)";
						$result = mysql_query($q_pilihan_b);
						if($result){
							$q_pilihan_c = "INSERT INTO pilihan (no, nama, d_soal) values(3, '$pilihan_c', $d_soal)";					
							$result = mysql_query($q_pilihan_c);
							if($result){
								$q_pilihan_d = "INSERT INTO pilihan (no, nama, d_soal) values(4, '$pilihan_d', $d_soal)";
								$result = mysql_query($q_pilihan_d);
								if($result){
									header("Location:../../admin_latihan_detail.php?id=".$soal_id);				
								}else{
									echo 'failed query :'.$q_pilihan_d ;
								}
							}else{
								echo 'failed query :'.$q_pilihan_c ;
							}
						}else{
							echo 'failed query :'.$q_pilihan_b ;
						}
						
					}else{
						echo 'failed query :'.$q_pilihan_a ;
					}
					
                }else{
                    echo 'failed query :'.$q ;
                }
                
                
        }else if($operation == "edit") {
			
                $result = mysql_query("UPDATE d_soal SET 
                        jawaban_pilihan_no = '$jawaban', 
                        pertanyaan = '$pertanyaan', 
                        soal_id = '$soal_id'
                        WHERE id=".$id);
                
                if($result){
						$result = mysql_query("UPDATE pilihan SET 
								nama = '$pilihan_a'
								WHERE d_soal=$id and no=1");
						if($result){
							$result = mysql_query("UPDATE pilihan SET 
									nama = '$pilihan_b'
									WHERE d_soal=$id and no=2");
							if($result){
								$result = mysql_query("UPDATE pilihan SET 
										nama = '$pilihan_c'
										WHERE d_soal=$id and no=3");
								if($result){
									$result = mysql_query("UPDATE pilihan SET 
											nama = '$pilihan_d'
											WHERE d_soal=$id and no=4");
									if($result){
										header("Location:../../admin_latihan_detail.php?id=".$soal_id);         										
									}else{
										
										echo 'failed '."UPDATE pilihan SET 
												nama = '$pilihan_d'
												WHERE d_soal=$id and no=4";
									}
								}else{
									
									echo 'failed '."UPDATE pilihan SET 
												nama = '$pilihan_c'
												WHERE d_soal=$id and no=3";
								}
							}else{
								
								echo 'failed '."UPDATE pilihan SET 
													nama = '$pilihan_b'
													WHERE d_soal=$id and no=2";		
							}
						}else{
							echo 'failed '."UPDATE pilihan SET 
												nama = '$pilihan_a'
												WHERE d_soal=$id and no=1";
						}

                }else{
					echo 'failed '."UPDATE d_soal SET 
										no = '$no',
										jawaban_pilihan_no = '$jawaban', 
										pertanyaan = '$pertanyaan', 
										soal_id = '$soal_id'
										WHERE id=".$id;
                }
                
                
        }else if($operation == "remove") {
                
                $result = mysql_query("DELETE FROM d_soal WHERE id=".$id);
        
                if($result){
					$result = mysql_query("DELETE FROM pilihan WHERE d_soal=".$id);
						
                    if($result){
						echo 'success';
					}
                }else{
                        echo 'failed';
                }
                
        }
?>
